from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
import os
import numpy as np


class multipleRegression(object):

    regressor_dict = {'Linear': LinearRegression(),
                      'KNeighbors': KNeighborsRegressor(),
                      'RandomForest': RandomForestRegressor(),
                      'DecisionTree': DecisionTreeRegressor()}

    def __init__(self, **kwargs):
        self.wd = kwargs.get('wd', os.getcwd())
        self.fileName = kwargs.get('fileName', '')

    @staticmethod
    def fit(**kwargs):
        _x = kwargs.get('x', np.array(()))
        _y = kwargs.get('y', np.array(()))
        _regressor = kwargs.get('regressor', None)

        _model = multipleRegression.regressor_dict[_regressor]
        _model.fit(_x, _y)
        
        return _model

    @staticmethod
    def rKFold(**kwargs):
        _x = kwargs.get('x', np.array(()))
        _y = kwargs.get('y', np.array(()))
        _regressor = kwargs.get('regressor', None)
        _n_s = kwargs.get('n_splits', 5)
        _n_r = kwargs.get('n_repeats', 10)
        _r_s = kwargs.get('random_state', None)
        _scoring = kwargs.get('scoring', None)

        _model = multipleRegression.regressor_dict[_regressor]
        _cv = RepeatedKFold(n_splits=_n_s, n_repeats=_n_r, random_state=_r_s)
        n_scores = cross_val_score(_model, _x, _y, scoring=_scoring, cv=_cv, n_jobs=-1)

        return n_scores
